import { defineStore } from "pinia";
import { getConfigApi } from "@/api/app";
import { LoginWayEnum } from "@/enums/appEnums";

interface AppSate {
  config: Record<string, any>;
  currentLoginWay: LoginWayEnum;
}
export const useAppStore = defineStore("appStore", {
  state: (): AppSate => ({
    config: {
      status: 1,
    },
    currentLoginWay: LoginWayEnum.DEFAULT,
  }),
  getters: {
    getWebsiteConfig: (state) => state.config.website || {},
    getLoginConfig: (state) => state.config.login || {},
    getTabbarConfig: (state) => state.config.tabbar || [],
    getStyleConfig: (state) => state.config.style || {},
    getH5Config: (state) => state.config.webPage || {},
    getCopyrightConfig: (state) => state.config.copyright || [],
  },
  actions: {
    getImageUrl(url: string) {
      return url.indexOf("http") ? `${this.config.domain}${url}` : url;
    },

    async getConfig() {
      getConfigApi().then((data) => {
        this.config = data;
        this.setCurrentLoginWay();
      });
    },
    setCurrentLoginWay(loginWay?: LoginWayEnum) {
      if (loginWay) {
        this.currentLoginWay = loginWay;
        return;
      } else {
        if (this.config.login.loginWay.includes(LoginWayEnum.ACCOUNT)) {
          this.currentLoginWay = LoginWayEnum.ACCOUNT;
        } else {
          this.currentLoginWay = this.config.login.loginWay[0];
        }
      }
    },
  },
});
